/////////////////////////////////////////////////////////////////////////////
// Name:        networkpanel.cpp
// Purpose:     
// Author:      Brant Young
// Modified by: 
// Created:     Wed Jun 20 23:06:24 2007
// RCS-ID:      
// Copyright:   (C) Victsoft 2007
// Licence:     GPL 2.0
/////////////////////////////////////////////////////////////////////////////


#include "wx/wx.h"

#include "networkpanel.h"
#include "cfgmgr.h"


/*!
 * NetworkPanel constructors
 */

NetworkPanel::NetworkPanel(wxWindow* p_parent) : wxPanel(p_parent, ID_NETWORK_PANEL, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER|wxTAB_TRAVERSAL)
{    
////@begin NetworkPanel content construction

    p_root_sizer = new wxBoxSizer(wxVERTICAL);
    this->SetSizer(p_root_sizer);

    p_conn_sizer_stc = new wxStaticBox(this, wxID_STATIC, _("Connection"));
    p_conn_sizer = new wxStaticBoxSizer(p_conn_sizer_stc, wxVERTICAL);
    p_root_sizer->Add(p_conn_sizer, 1, wxGROW|wxALL, 5);

    p_direct_rbtn = new wxRadioButton( this, ID_DIRECT_RBTN, _("Direct connection to the Internet"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP );
    p_direct_rbtn->SetValue(false);
    p_conn_sizer->Add(p_direct_rbtn, 0, wxGROW|wxALL, 5);

    p_auto_rbtn = new wxRadioButton( this, ID_AUTO_RBTN, _("Auto-detect proxy settings for this network"), wxDefaultPosition, wxDefaultSize, 0 );
    p_auto_rbtn->SetValue(true);
    p_conn_sizer->Add(p_auto_rbtn, 0, wxGROW|wxALL, 5);

    p_manual_rbtn = new wxRadioButton( this, ID_MANUAL_RBTN, _("Manual Proxy configuration :"), wxDefaultPosition, wxDefaultSize, 0 );
    p_manual_rbtn->SetValue(false);
    p_conn_sizer->Add(p_manual_rbtn, 0, wxGROW|wxALL, 5);

    p_proxy_sizer = new wxFlexGridSizer(3, 5, 0, 0);
    p_proxy_sizer->AddGrowableRow(0);
    p_proxy_sizer->AddGrowableRow(1);
    p_proxy_sizer->AddGrowableRow(2);
    p_proxy_sizer->AddGrowableCol(2);
    p_conn_sizer->Add(p_proxy_sizer, 0, wxGROW|wxLEFT|wxRIGHT, 5);

    p_proxy_sizer->Add(20, 5, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_http_rbtn = new wxRadioButton( this, ID_HTTP_RBTN, _("HTTP Proxy:"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP );
    p_http_rbtn->SetValue(true);
    p_proxy_sizer->Add(p_http_rbtn, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxRIGHT|wxTOP|wxBOTTOM, 5);

    p_http_ip_text = new wxTextCtrl( this, ID_HTTP_IP_TEXT, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
    p_proxy_sizer->Add(p_http_ip_text, 1, wxGROW|wxGROW|wxALL, 5);

    wxStaticText* itemStaticText11 = new wxStaticText( this, wxID_STATIC, _("Port:"), wxDefaultPosition, wxDefaultSize, 0 );
    p_proxy_sizer->Add(itemStaticText11, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_http_port_text = new wxTextCtrl( this, ID_HTTP_PORT_TEXT, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
    p_proxy_sizer->Add(p_http_port_text, 1, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_proxy_sizer->Add(20, 5, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_socks4_rbtn = new wxRadioButton( this, ID_SOCKS4_RBTN, _("SOCKS v4 Proxy:"), wxDefaultPosition, wxDefaultSize, 0 );
    p_socks4_rbtn->SetValue(false);
    p_proxy_sizer->Add(p_socks4_rbtn, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxRIGHT|wxTOP|wxBOTTOM, 5);

    p_socks4_ip_text = new wxTextCtrl( this, ID_SOCKS4_IP_TEXT, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
    p_proxy_sizer->Add(p_socks4_ip_text, 1, wxGROW|wxGROW|wxALL, 5);

    wxStaticText* itemStaticText16 = new wxStaticText( this, wxID_STATIC, _("Port:"), wxDefaultPosition, wxDefaultSize, 0 );
    p_proxy_sizer->Add(itemStaticText16, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_socks4_port_text = new wxTextCtrl( this, ID_SOCKS4_PORT_TEXT, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
    p_proxy_sizer->Add(p_socks4_port_text, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_proxy_sizer->Add(20, 5, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_socks5_rbtn = new wxRadioButton( this, ID_SOCKS5_RBTN, _("SOCKS v5 Proxy:"), wxDefaultPosition, wxDefaultSize, 0 );
    p_socks5_rbtn->SetValue(false);
    p_proxy_sizer->Add(p_socks5_rbtn, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxRIGHT|wxTOP|wxBOTTOM, 5);

    p_socks5_ip_text = new wxTextCtrl( this, ID_SOCKS5_IP_TEXT, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
    p_proxy_sizer->Add(p_socks5_ip_text, 1, wxGROW|wxGROW|wxALL, 5);

    wxStaticText* itemStaticText21 = new wxStaticText( this, wxID_STATIC, _("Port:"), wxDefaultPosition, wxDefaultSize, 0 );
    p_proxy_sizer->Add(itemStaticText21, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    p_socks5_port_text = new wxTextCtrl( this, ID_SOCKS5_PORT_TEXT, _T(""), wxDefaultPosition, wxDefaultSize, 0 );
    p_proxy_sizer->Add(p_socks5_port_text, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

////@end NetworkPanel content construction
    
    InitEvents();
    _init_proxy_cfg();
    _init_conn_cfg();
}


/*!
 * NetworkPanel destructor
 */

NetworkPanel::~NetworkPanel()
{
////@begin NetworkPanel destruction
////@end NetworkPanel destruction
}


void NetworkPanel::_init_conn_cfg() {
	// wxString str;
	// bool flag;
	long integer;
	
	ConfigManager& cfg_mgr = ConfigManager::get();

    wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, 0);
    event.SetEventObject(this);
    
	integer = cfg_mgr.Read(_T("/Network/ConnectType"),1); // 0 - direct, 1 - auto, 2 - manual
	switch (integer) {
		case 0:
			p_direct_rbtn->SetValue(true);
			event.SetId(ID_DIRECT_RBTN);
			break;
		case 1:
			p_auto_rbtn->SetValue(true);
			event.SetId(ID_AUTO_RBTN);
			break;
		case 2:
			p_manual_rbtn->SetValue(true);
			event.SetId(ID_MANUAL_RBTN);
			break;
		default :
			p_auto_rbtn->SetValue(true);
			event.SetId(ID_AUTO_RBTN);
	}
	
	ProcessEvent(event);
}


void NetworkPanel::_init_proxy_cfg() {
	wxString str;
	long integer;
	
	ConfigManager& cfg_mgr = ConfigManager::get();

    wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, 0);
    event.SetEventObject(this);
    
    integer = cfg_mgr.Read(_T("/Network/ProxyType"),long(0)); // 0 - http, 1 - socks4, 2 - socks5
	switch (integer) {
		case 0:
			p_http_rbtn->SetValue(true);
			event.SetId(ID_HTTP_RBTN);
			break;
		case 1:
			p_socks4_rbtn->SetValue(true);
			event.SetId(ID_SOCKS4_RBTN);
			break;
		case 2:
			p_socks5_rbtn->SetValue(true);
			event.SetId(ID_SOCKS5_RBTN);
			break;
		default :
			p_http_rbtn->SetValue(true);
			event.SetId(ID_HTTP_RBTN);
	}
	
	ProcessEvent(event);
	
	str = cfg_mgr.Read(_T("/Network/HttpProxyIP"));
	p_http_ip_text->SetValue(str);
	str = cfg_mgr.Read(_T("/Network/Socks4ProxyIP"));
	p_socks4_ip_text->SetValue(str);
	str = cfg_mgr.Read(_T("/Network/Socks5ProxyIP"));
	p_socks5_ip_text->SetValue(str);
	str = cfg_mgr.Read(_T("/Network/HttpProxyPort"));
	p_http_port_text->SetValue(str);
	str = cfg_mgr.Read(_T("/Network/Socks4ProxyPort"));
	p_socks4_port_text->SetValue(str);
	str = cfg_mgr.Read(_T("/Network/Socks5ProxyPort"));
	p_socks5_port_text->SetValue(str);
}


bool NetworkPanel::set_cfg() {
	ConfigManager& cfg_mgr = ConfigManager::get();

	cfg_mgr.Write(_T("/Network/HttpProxyIP"), p_http_ip_text->GetValue());
	cfg_mgr.Write(_T("/Network/Socks4ProxyIP"), p_socks4_ip_text->GetValue());
	cfg_mgr.Write(_T("/Network/Socks5ProxyIP"), p_socks5_ip_text->GetValue());
	cfg_mgr.Write(_T("/Network/HttpProxyPort"), p_http_port_text->GetValue());
	cfg_mgr.Write(_T("/Network/Socks4ProxyPort"), p_socks4_port_text->GetValue());
	cfg_mgr.Write(_T("/Network/Socks5ProxyPort"), p_socks5_port_text->GetValue());
	
	if (p_direct_rbtn->GetValue()) {
		cfg_mgr.Write(_T("/Network/ConnectType"),0);
	} else if (p_auto_rbtn->GetValue()) {
		cfg_mgr.Write(_T("/Network/ConnectType"),1);
	} else if (p_manual_rbtn->GetValue()) {
		cfg_mgr.Write(_T("/Network/ConnectType"),2);
	} else {
		cfg_mgr.Write(_T("/Network/ConnectType"),1);
	}
	
	if (p_http_rbtn->GetValue()) {
		cfg_mgr.Write(_T("/Network/ProxyType"),0);
	} else if (p_socks4_rbtn->GetValue()) {
		cfg_mgr.Write(_T("/Network/ProxyType"),1);
	} else if (p_socks5_rbtn->GetValue()) {
		cfg_mgr.Write(_T("/Network/ProxyType"),2);
	} else {
		cfg_mgr.Write(_T("/Network/ProxyType"),0);
	}
	
	return true;
}

void NetworkPanel::InitEvents() {
	this->Connect(ID_DIRECT_RBTN, wxEVT_COMMAND_RADIOBUTTON_SELECTED, 
			(wxObjectEventFunction)&NetworkPanel::OnConnectionType);
	this->Connect(ID_AUTO_RBTN, wxEVT_COMMAND_RADIOBUTTON_SELECTED, 
			(wxObjectEventFunction)&NetworkPanel::OnConnectionType);
	this->Connect(ID_MANUAL_RBTN, wxEVT_COMMAND_RADIOBUTTON_SELECTED, 
			(wxObjectEventFunction)&NetworkPanel::OnConnectionType);
	

	this->Connect(ID_HTTP_RBTN, wxEVT_COMMAND_RADIOBUTTON_SELECTED, 
			(wxObjectEventFunction)&NetworkPanel::OnConnectionType);
	this->Connect(ID_SOCKS4_RBTN, wxEVT_COMMAND_RADIOBUTTON_SELECTED, 
			(wxObjectEventFunction)&NetworkPanel::OnConnectionType);
	this->Connect(ID_SOCKS5_RBTN, wxEVT_COMMAND_RADIOBUTTON_SELECTED, 
			(wxObjectEventFunction)&NetworkPanel::OnConnectionType);
}

void NetworkPanel::OnConnectionType(wxCommandEvent& event) {
	wxSizerItemList& items = p_proxy_sizer->GetChildren();
	int d_array[6] = {0};
	int e_array[6] = {0};
	
	switch (event.GetId()) {
	case ID_DIRECT_RBTN:
	case ID_AUTO_RBTN:
		for (int i=0;i<items.GetCount();i++) {
			wxWindow* wnd = items.Item(i)->GetData()->GetWindow();
			
			if (NULL==wnd) continue;
			
			wnd->Disable();
		}
		break;
	case ID_MANUAL_RBTN:
		for (int i=0;i<items.GetCount();i++) {
			wxWindow* wnd = items.Item(i)->GetData()->GetWindow();
			
			if (NULL==wnd) continue;
			
			wnd->Enable();
		}
		
		_init_proxy_cfg();
		break;
	case ID_HTTP_RBTN:
		d_array[0]=7; d_array[1]=8; d_array[2]=9; d_array[3]=12; d_array[4]=13; d_array[5]=14;
		e_array[0]=2; e_array[1]=3; e_array[2]=4;
		for (int i=0;i<6;i++) {
			wxWindow* wnd = items.Item(d_array[i])->GetData()->GetWindow();
			
			if (NULL==wnd) continue;
			
			wnd->Disable();
			
			if (i<3) {
				wnd = items.Item(e_array[i])->GetData()->GetWindow();
				if (NULL==wnd) continue;
				wnd->Enable();
			}
		}
		break;
	case ID_SOCKS4_RBTN:
		d_array[0]=2; d_array[1]=3; d_array[2]=4; d_array[3]=12; d_array[4]=13; d_array[5]=14;
		e_array[0]=7; e_array[1]=8; e_array[2]=9;
		for (int i=0;i<6;i++) {
			wxWindow* wnd = items.Item(d_array[i])->GetData()->GetWindow();
			
			if (NULL==wnd) continue;
			
			wnd->Disable();
			
			if (i<3) {
				wnd = items.Item(e_array[i])->GetData()->GetWindow();
				if (NULL==wnd) continue;
				wnd->Enable();
			}
		}
		break;
	case ID_SOCKS5_RBTN:
		for (int i=0;i<6;i++) {
			d_array[0]=2; d_array[1]=3; d_array[2]=4; d_array[3]=7; d_array[4]=8; d_array[5]=9;
			e_array[0]=12; e_array[1]=13; e_array[2]=14;
			wxWindow* wnd = items.Item(d_array[i])->GetData()->GetWindow();
			
			if (NULL==wnd) continue;
			
			wnd->Disable();
			
			if (i<3) {
				wnd = items.Item(e_array[i])->GetData()->GetWindow();
				if (NULL==wnd) continue;
				wnd->Enable();
			}
		}
		break;
	}
}
